package logic

import (
	"context"
	"errors"
	"user-regist/models/mysql"
	"user-regist/pkg"

	"user-regist/userrpc/internal/svc"
	"user-regist/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *UserRegisterLogic) UserRegister(in *userrpc.UserRegisterRequest) (*userrpc.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line

	user := &mysql.User{
		Account:  in.Account,
		Password: pkg.Md5(in.Password),
	}
	err := user.UserAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("用户注册失败")
	}

	return &userrpc.UserRegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
